package com.geese.filter;

import com.geese.filter.rsa.RSAForPrivateCodec;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.security.NoSuchAlgorithmException;
import java.util.Properties;

/**
 * Created by admin on 2016/7/22.
 */
public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {

    private static Logger logger=Logger.getLogger(InstantiationTracingBeanPostProcessor.class);

    private static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMDCnJGYcDRT9FOZ\n" +
            "oYyIIeNhcnxl3QgIYEuF05BZNS6kp0DU4drTMaQ6HiEv+a1YFnhWYlBNdm77zEFU\n" +
            "y/KYIFLM05B9Wn7hVVde1rCdCTt/YlvXw763nuaYSFpj8Gz++PkB8tiM9uYY224N\n" +
            "Bz/gRE/1yKzOUYLUCLkeL2ShCB93AgMBAAECgYAK98fS3Pqdb516WEaB6YkMGBJc\n" +
            "k6s09s0L3wx/rSPCiKnfnX1QZy3u60OlncY2H8n/hOLJMO8pN3iSyxFjt1Y3Zpbl\n" +
            "gPh4jH4WDRcv+XdeEHfiT0Jcrapl+HEKPemw8TyB5vMyULt/xCLV266BV2JZbjLI\n" +
            "2ZjWUHUae1vHeEJaIQJBAOXG5Z2GoeDiMC0QFzo/ENRumzmciZSE2yF5rIMX4u9Q\n" +
            "cMHJNBMrRPvfOKVSfgC7uf5jHj/nVruTmV5wkv8CVacCQQDWwj5HAw9Bwxgl0Gls\n" +
            "K+OxnWeRRPASe598pFfcmBX6I8BgN0kstoTsM5xHEmVWzT/osMuPmnCoY+WR3ldu\n" +
            "aMGxAkEAkQdCJHQ2tm9tgvPWBVCK+do6JVnVSKW2feGDblbkSLKYCOhK8xJrfyJ8\n" +
            "aXSwzRuwII1FKYaFqu1Vb1NUpJo/nwJARhLLT1BoQgTudCg0vd70RJgfmv7rdpxv\n" +
            "xbO6F3hoc14G/14KulQzuv58F5ShDFmcaMiHIyw5ZjhyR85MqPH1gQJBAJef7bB9\n" +
            "toLzHudgtMwCS1IEIwTTxd5hMZnb63OffHv+cy12+DEbuQQb7HnTTBW9bg1dgLJi\n" +
            "2FwB5UOdMi6drOM=";

    private static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAwpyRmHA0U/RTmaGMiCHjYXJ8\n" +
            "Zd0ICGBLhdOQWTUupKdA1OHa0zGkOh4hL/mtWBZ4VmJQTXZu+8xBVMvymCBSzNOQ\n" +
            "fVp+4VVXXtawnQk7f2Jb18O+t57mmEhaY/Bs/vj5AfLYjPbmGNtuDQc/4ERP9cis\n" +
            "zlGC1Ai5Hi9koQgfdwIDAQAB";

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if(event.getApplicationContext().getParent() == null){
            if (logger.isDebugEnabled()){
                logger.debug("spring容器初始化完成，开始进行配置初始化");
            }

            Properties properties = (Properties) event.getApplicationContext().getBean("configProperties");

            //白名单列表
            String whiteListFlag = properties.getProperty("white.list.flag", "1");
            String whitelist = properties.getProperty("white.list", "127.0.0.1");
            String verifySignFlag = properties.getProperty("verify.sign.flag", "1");
            String interfaceWhiteList = properties.getProperty("interface.white.list", "");
            if (logger.isDebugEnabled()){
                logger.debug("**************************init config*****************************");
                logger.debug("whitelist=" + whitelist);
                logger.debug("whiteListFlag=" + whiteListFlag);
                logger.debug("verifySignFlag=" + verifySignFlag);
                logger.debug("interfaceWhiteList=" + interfaceWhiteList);
                logger.debug("******************************************************************");
            }

            //初始化RSA密钥对
            try {
                RSAForPrivateCodec privateCode = new RSAForPrivateCodec(privateKey, publicKey);

                //初始化安全过滤器
                SecurityFilter.init(whiteListFlag, whitelist, verifySignFlag, privateCode, interfaceWhiteList);
            } catch (NoSuchAlgorithmException e) {
                logger.error("密钥初始化失败", e);
            }

            if (logger.isDebugEnabled()){
                logger.debug("配置初始化完成");
            }
        }
    }
}
